//logs.js
var util = require('../../utils/util.js')
Page({
   data: {
    item: {
      display: 'hide',
      error_info: '请添加比赛信息',
    },
    navigator: {
      nav_one: ['/image/nav1.png',''],
      nav_two: ['/image/nav6.png', 'nav1'],
      nav_three: ['/image/nav2.png', ''],
      nav_type: '/pages/battle/battle'
    },
    
    battle_type_info : '当前比赛项目',
    battle_type_list:['','八球','斯诺克','九球'],
    battle_type_name:'八球',

     event: {
       event_name       :'',
       event_logo:'',
     },
     battle_id   : ''
  },
  //获取赛事信息
  onShow:function(){
      //赛事信息
      var battle_info = wx.getStorageSync('battle_info');
      var battle_type = wx.getStorageSync('battle_type') || '0';
      if(battle_type == 0){
        battle_type = wx.getStorageSync('event_info').event_type;
      }
      
      var nav_type = battle_info.battle_type == 2 ? '/pages/snookerbattle/snookerbattle' : '/pages/battle/battle';
  
      this.setData({navigator: {nav_type: nav_type}});
    

      this.setData({
             battle_id   :battle_info.battle_id,
             event:{
               event_name: wx.getStorageSync('event_info').event_name,
               event_logo: wx.getStorageSync('event_info').event_logo
             },
             
             battle_type_info: battle_type == 0 ? '请新建比赛' :'当前比赛项目',
             battle_type_name:this.data.battle_type_list[battle_type]
      })
  },  
   doScanCode:function(){
    var that = this;
    if(this.remindAddBattleInfo()){
        wx.scanCode({
            success: (res) => {
                console.log(res.result);
                that.saveLoginCode(res.result);
            }
        })
     }
  },
  startScoring:function(){
    var battle_info = wx.getStorageSync('battle_info');
    var nav_type = battle_info.battle_type == 2 ? '/pages/snookerbattle/snookerbattle' : '/pages/battle/battle';

    wx.redirectTo({
      url: nav_type
    })
  },
  addBattleInfo:function(){
    if (this.remindAddBattleInfo()) {
      wx.navigateTo({
        url: "/pages/editbattle/editbattle"
      })
    }
  },
  //如果对局信息为空提示用户添加对局信息
    remindAddBattleInfo:function(){
        var that =this;
        var battle_info = wx.getStorageSync('battle_info');
      
        if(!battle_info){
            that.setData({
              item: {
                 display    :'show',
                 error_info :'请添加比赛信息'
              }
            })
            this.closeErrorInfo();
            return false; 
        }else{
            return true;
        }
    },

     //定时器关闭提示信息
    closeErrorInfo:function(){
        var that = this;
        setTimeout(function(){
          that.setData({
              item: {display:'hide'}
              })
        },'1000')
    },

     //如果对局信息为空提示用户添加对局信息
    remindAddBattleInfo:function(){
        var that =this;
        var battle_info = wx.getStorageSync('battle_info');
      
        if(!battle_info){
            that.setData({
              item: {
                 display    :'show',
                 error_info :'请添加对局信息'
              }
            })
            this.closeErrorInfo();
            return false; 
        }else{
            return true;
        }
    },

    updateGameType:function(){
      var that = this;
      wx.showActionSheet({
        itemList: ['八球', '斯诺克', '九球'],
        success: function (res) {
          if (res.tapIndex || res.tapIndex === 0) {
            var itemList = [1, 2, 3];
            var point = itemList[res.tapIndex];
            that.setData({
              battle_type_name: that.data.battle_type_list[Number(point)]
            })
            wx.setStorage({
                key: "battle_type",
                data: point
            })

            wx.showToast({
              title: '修改比赛类型成功，请新建一场' + that.data.battle_type_name+'比赛',
              icon: 'success',
              duration: 2000
            })
          }
        }
      })
    },

     //保存扫描后的登陆码
  saveLoginCode:function(login_code){
         var that = this;
         console.log(login_code);
      //添加登录码
         var url  = getApp().data.servser+'/Battle/addLoginCode';
          wx.request({
            url: url,
            data: {
              'id'         : that.data.battle_id,
              'session_id' : wx.getStorageSync('session_id'),
              'login_code' : login_code,
              'event_id'   : wx.getStorageSync('event_id')
            },
            header: {
                'content-type': 'application/json'
            },
            method : "GET",
            success: function(res) {
                console.log(res);
            }
          })
  },
})
